package com.guigu.service.impl;

import com.guigu.dto.UserDTO;
import com.guigu.entity.role;
import com.guigu.entity.t_account;
import com.guigu.entity.t_account_role;
import com.guigu.mapper.AccountMapper;
import com.guigu.service.UserService;
import com.guigu.vo.UserVO1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public List<UserVO1> query() {
        return this.accountMapper.query();
    }

    @Override
    public List<role> queryrole() {
        return this.accountMapper.queryrole();
    }

    @Override
    public void adduser(UserDTO dto) {
        try {
            System.out.println(dto.toString());
            dto.setBirthday(new Date());
            this.accountMapper.adduser(dto);
            t_account account=new t_account();
            account.setAccount(dto.getUser_name());
            account.setUser_id(dto.getUser_Id());
            account.setPassword("8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
            account.setCreatetime(new Date());
            this.accountMapper.addAccount(account);
            dto.setUser_Id(account.getAc_id());
            this.accountMapper.addrole(dto);
        } catch (Exception e) {
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw e;
        }
    }

    @Override
    public void updateuser(UserDTO dto) {
        this.accountMapper.updateuser(dto);
    }

    @Override
    public void Del(Integer id) {
        this.accountMapper.Del(id);
    }

    @Override
    public List<role> rolename(String id) {
        return this.accountMapper.rolename(id);
    }

    @Override
    public List<role> rolename1(int id) {
        return this.accountMapper.rolename1(id);
    }

    @Override
    public void quanxainfo(t_account_role role) {
        int id=this.accountMapper.queryacount_id(role.getAc_id());
        System.out.println(id);
        role.setAc_id(id);
        if(role.getAdd()!=null&&role.getAdd().size()>0){
            this.accountMapper.addquanxainfo(role);
        }
        if(role.getDel()!=null&&role.getDel().size()>0){
            this.accountMapper.Delquanxainfo(role);
        }
    }
}
